package Servlets;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.*;
//import java.net.*;
import static java.lang.Math.*;
import Email.GMailManager;
//import java.util.Random;

import BeanManager.UserManager;
import Beans.User;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author r_uddie
 */
public class ForgotPasswordServlet extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
                   
        int i,num;double x;char c;
        StringBuilder securitycode = new StringBuilder();
        String encryptedPassword;
        
        String email = request.getParameter("username");
        String submit = request.getParameter("submit");
        String security = request.getParameter("security");
        String securitypass = request.getParameter("securitypass");
        //String newpassword = request.getParameter("newpassword");
        //String confirmpassword = request.getParameter("confirmpassword");
        
        
        if(submit != null)
        {
            User user = UserManager.getUser(email);
          
            if(user != null)
            {
                //generate a security code
                for(i=0;i<16;i++)
                {
                    x=random();
                    num=(int)(x/0.0385);
                    c=(char)(num+97);
                    securitycode.append(c);           
                }
         
                GMailManager.sendMail( "Security Code" , "This e-mail is sent upon your request to securely update your password.\n\n Your Security Code is " + securitycode , email );
        
                out.println("<form method=\"POST\" action=\"ForgotPasswordServlet\" > A Security Code has been sent to your e-mail address.<br />"+
                    "You may have received it as SPAM.<br /><br />" + "<input type=\"hidden\" name=\"securitypass\" value=" + securitycode.toString() + " />"+
                    "Security Code : <input type=\"password\" name=\"security\" value=\"\" />"+ "<input type=\"hidden\" name=\"username\" value="+ email + " />"+
                    "<input type=\"submit\" value=\"Submit\" /></form>");
            }
            else
                out.println("Invalid Username!");
        }
        else
        {
            if(security == null)
                out.println("Illegal Entry! Submit Username properly.");
            else 
            {
                if(security.equals(securitypass))
                {
                    //generate new password
                    for(i=0;i<16;i++)
                    {
                        x=random();
                        num=(int)(x/0.0385);
                        c=(char)(num+97);
                        securitycode.append(c);           
                    }
                    encryptedPassword = UserManager.encrypt( UserManager.encrypt( securitycode.toString() ) ) ;
                    GMailManager.sendMail( "New Password" , "This e-mail confirms that you have passed the security test.\n\n Your new Password is " + securitycode , email );
            
                    out.println( UserManager.updateUser(email, "PASSWORD", encryptedPassword) + "The new password has been sent to your e-mail address.<br />" + 
                                "You may have received it as SPAM. Please change your password immediately by first logging in.<br /><br />" +
                                "<form method=\"POST\" action=\"ChangePasswordServlet\" >"+
                                "Username : "+ email +"<input type=\"hidden\" name=\"username\" value=" + email + " />" + "<br />"+
                                "Password : <input type=\"password\" name=\"password\" value=\"\" /> <br />"+
                                "<input type=\"submit\" value=\"Change Password\" name=\"changepassword\" /></form>");     
                }
                else
                    out.println("Error: Security Code did not match!");
            }
        }
        
        
        out.close();
        
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException 
    {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
